package com.zhoudy.segmetqueue;



import java.util.Arrays;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TransferDatTest {

    private static final int handlerThreads = 10;

    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        int arrLength = 1000;

        String[] data = new String[arrLength];
        for (int i = 0; i < arrLength; i++) {
            data[i] = "TestData".concat(String.valueOf(i));
        }
        //迁移前
        System.out.println("老数据：" + Arrays.asList(data));

        TransferDat transferDat = new TransferDat();
        //切分数据块
        transferDat.segmentData(data);

        //迁移开始并计数
        CountDownLatch downLatch = new CountDownLatch(handlerThreads);
        ExecutorService es = Executors.newFixedThreadPool(handlerThreads);
        //模拟并发迁移
        try {
            for (int i = 0; i < handlerThreads; i++) {
                es.submit(new Thread(new Worker(transferDat, downLatch)));
            }
            downLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        es.shutdown();

        //迁移后
        System.out.println("Finished:" + transferDat.isFinish());
        System.out.println("新数据：" + transferDat.getNewDataContainer());
        System.out.println("耗时：" + (System.currentTimeMillis() - start) + "ms");
    }

}
